﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;

namespace ZHDJ.MobileInterface
{
    /// <summary>
    /// PrintMachineInterface 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class PrintMachineInterface : System.Web.Services.WebService
    {
        /// <summary>
        /// 人员信息接口
        /// </summary>
        /// <param name="beginTime">查询时间[yyyy-MM-dd]</param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        [WebMethod]
        public List<UserInfo> GetOrgMembersInfo(string beginTime, out string errMsg)
        {
            string beginTimeStr = "";
            errMsg = "";
            try
            {
                beginTimeStr = Convert.ToDateTime(beginTime).ToString("yyyy-MM-dd") + " 00:00:00";
            }
            catch
            {
                errMsg = "查询时间格式不正确";
            }

            string url = CommonUtils.AppAddress;
            string rootUrl = url + CommonUtils.FileServerRootUrl + "/UploadFiles/";
            List<UserInfo> userList = new List<UserInfo>();
            UserInfo userTemp = new UserInfo();
            DataSet ds = InterfaceHelper.GetOrgMembersInfo(beginTimeStr);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    userTemp = new UserInfo();  
                    userTemp.BranchID = dr["BranchID"].ToString();
                    userTemp.BranchName = dr["BranchName"].ToString();
                    userTemp.Gender = dr["Gender"].ToString();
                    userTemp.IdentityCard  = dr["IdentityCard"].ToString();
                    userTemp.ImageUrl = rootUrl + dr["ImageUrl"].ToString();
                    userTemp.MobileNo = dr["MobileNo"].ToString();
                    userTemp.opType = dr["opType"].ToString();
                    userTemp.UserID = dr["UserID"].ToString();
                    userTemp.UserName = dr["UserName"].ToString();
                    userTemp.CardID = dr["PartyCardNo"].ToString();
                    userList.Add(userTemp);
                }
            }

            return userList;
        }

        /// <summary>
        /// 打卡机提交数据接口
        /// </summary>
        /// <param name="MachineCode">机器码</param>
        /// <param name="loginID">人员ID</param>
        /// <param name="PutTime">打卡时间</param>
        /// <returns></returns>
        [WebMethod]
        public string MachinePut(string MachineCode, string loginID, string PutTime)
        {
            DateTime dtPutTime; 

            try
            {
                dtPutTime = Convert.ToDateTime(PutTime);
            }
            catch
            {
                return "打卡时间格式不正确";
            }

            if (string.IsNullOrEmpty(MachineCode))
                return "机器码不能为空";

            //考虑打卡机器可能与服务器时间不一致，暂时注释
            //DateTime maxTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
            //if (dtPutTime > maxTime)
            //    return "打卡时间不正确";

            //用户是否存在
            if (!InterfaceHelper.CheckLoginIDCountEx(loginID))
                return "用户不存在";

            string putDay = dtPutTime.ToString("yyyy-MM-dd");
            string putWeek = CommonUtils.WeekOfYear("1", dtPutTime).ToString();
            InterfaceHelper.MachinePutSubmit(MachineCode, dtPutTime.Year.ToString(), putWeek, putDay, loginID, dtPutTime);
               
            return "操作成功";
        }  
    }


    public class UserInfo
    {
        //（登录ID，姓名，组织ID，组织名称，性别，身份证号，手机号，照片路径,类型[1 删除 0 新增/修改
        public string UserID { get; set; }
        public string UserName { get; set; }
        public string BranchID { get; set; }
        public string BranchName { get; set; }
        public string Gender { get; set; }
        public string IdentityCard { get; set; }
        public string MobileNo { get; set; }
        public string ImageUrl { get; set; }
        public string opType { get; set; }
        public string CardID { get; set; }
    }
}
